package org.dev.lms.controller;

import org.dev.lms.service.BookSubscribeService;
import org.dev.lms.entity.BookSubscribe;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dev.common.core.page.PaginAtion;
import org.dev.common.core.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.dev.common.core.aop.OperLog;

import java.util.List;

@RestController
@RequestMapping("/bookSubscribe")
public class BookSubscribeController {
    @Autowired
    public BookSubscribeService bookSubscribeService;

    /**
     * 保存和修改公用的
     * @param bookSubscribe  传递的实体
     * @return ResponseResult转换结果
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    @OperLog(description = "bookSubscribe保存和修改")
    public ResponseResult<String> save(@RequestBody BookSubscribe bookSubscribe) {
        boolean result = false;
        try {
            if(bookSubscribe.getId()!=null){
                result = bookSubscribeService.updateById(bookSubscribe);
            }else{
                result = bookSubscribeService.save(bookSubscribe);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        if (result)
            return ResponseResult.success();
        return ResponseResult.error();
    }
    /**
     * 获取对象信息
     * @param id
     * @return
     */
    @GetMapping("/get")
    @OperLog(description = "bookSubscribe获取对象信息")
    public  ResponseResult<BookSubscribe> get(@RequestParam("id") Long id) {
        BookSubscribe bookSubscribe = bookSubscribeService.getById(id);
        return ResponseResult.success(bookSubscribe);
    }

    /**
     * 删除对象信息
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @OperLog(description = "bookSubscribe删除对象信息")
    public ResponseResult<String> delete(@RequestBody List<Long> ids) {
        try {
            bookSubscribeService.removeByIds(ids);
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 分页查询数据信息
     * @param ids
     * @param bookSubscribe
     * @return
     */
    @RequestMapping("/list")
    @OperLog(description = "bookSubscribe分页查询数据信息")
    public ResponseResult<IPage<BookSubscribe>> list(BookSubscribe bookSubscribe , PaginAtion pagination) {
        Page page = pagination.getPage();
        return ResponseResult.success(bookSubscribeService.page(page, new QueryWrapper<BookSubscribe>(bookSubscribe)));
    }
}